/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb;

/** 
 * 
 * 
 * @author jdp
 */
public class Method 
    extends Base
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb","xb:method");

    /**
     * <p> Construct an XB reference for a reflected method. </p>
     * 
     * @param jmethod An existing method
     * @return An XB reference to the argument method
     */
    public final static java.lang.String Ref4(java.lang.reflect.Method jmethod){
        if (null == jmethod)
            throw new Error.Argument("Null argument.");
        else {
            java.lang.String tmp;
            syntelos.iou.chbuf strbuf = new syntelos.iou.chbuf("method:class:utf8:");
            java.lang.Class jclass = jmethod.getDeclaringClass();
            tmp = jclass.getName().replace('.','/');
            tmp = syntelos.iou.url.encode(tmp);
            strbuf.append(tmp);
            strbuf.append("?name=");
            tmp = jmethod.getName();
            tmp = syntelos.iou.url.encode(tmp);
            tmp = syntelos.iou.chbuf.cat("utf8:",tmp);
            tmp = syntelos.iou.url.encode(tmp);
            strbuf.append(tmp);
            strbuf.append("?type=");
            tmp = Type.Type4(jmethod);
            tmp = syntelos.iou.url.encode(tmp);
            tmp = syntelos.iou.chbuf.cat("utf8:",tmp);
            tmp = syntelos.iou.url.encode(tmp);
            strbuf.append(tmp);
            return strbuf.toString();
        }
    }


    public Method(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
        super(doc,name);
    }
    public Method(org.w3c.dom.Document doc){
        this(doc,NAME);
    }
    public Method(){
        this(DOC_NIL);
    }

    public Method getMethod(){
        return this;
    }
    public syntelos.xb.at.Code getAttributeCode(){
        return this.getAttributesChild().getCode();
    }
    public syntelos.xb.op.Code getOpCode(){
        return this.getAttributeCode().getOpCode();
    }
    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
        return this.getAttributeCode().getAttributeLocalVariableTable();
    }
    public syntelos.xb.at.LineNumberTable getAttributeLineNumberTable(){
        return this.getAttributeCode().getAttributeLineNumberTable();
    }
    public syntelos.xb.at.Variable getVar(IO.Uri var_uri){

        this.getOpCode();/*(make sure this exists before possibly
                          * creating locals)
                          */
        return this.getAttributeLocalVariableTable().getVar(var_uri);
    }
    public boolean isStatic(){
        Access access = this.getAccess(false);
        if (null == access)
            return false;
        else
            return access.isStatic();
    }
    public boolean isPrivate(){
        Access access = this.getAccess(false);
        if (null == access)
            return false;
        else
            return access.isPrivate();
    }
    public boolean isProtected(){
        Access access = this.getAccess(false);
        if (null == access)
            return false;
        else
            return access.isProtected();
    }
    public boolean isPublic(){
        Access access = this.getAccess(false);
        if (null == access)
            return true;
        else
            return access.isPublic();
    }
    public boolean isInterface(){
        return this.getDeclaringClass().isInterface();
    }
    public boolean isSpecial(){
        Name name = this.getName(false);
        if (null == name)
            return false;
        else {
            java.lang.String string = name.getNameConstString();
            if (null == string)
                return false;
            else 
                return ('<' == string.charAt(0) &&
                        '>' == string.charAt(string.length()-1));
        }
    }
    public Name getName(){
        return this.getName(true);
    }
    public Name getName(boolean create){
        Name name = (Name)this.getChildByName("name");
        if (null == name && create){
            name = new Name();
            this.appendChild(name);
        }
        return name;
    }
    public Attributes getAttributesChild(){
        Attributes attributes = (Attributes)this.getChildByName("attributes");
        if (null == attributes){
            attributes = new Attributes();
            this.appendChild(attributes);
        }
        return attributes;
    }
    public syntelos.xb.at.SourceFile getClassAttributeSourceFile(){
        return this.getDeclaringClass().getAttributeSourceFile();
    }
    public Access getAccess(){
        return this.getAccess(true);
    }
    public Access getAccess(boolean create){
        Access access = (Access)this.getChildByName("access");
        if (null == access && create){
            access = new Access();
            this.appendChild(access);
        }
        return access;
    }
    public Type.Descriptor getType(){
        Type.Descriptor type = (Type.Descriptor)this.getChildByName("type.descriptor");
        if (null == type){
            type = new Type.Descriptor();
            this.appendChild(type);
        }
        return type;
    }

    public void readCF(IO.Uri src, CF.Reader reader)
        throws java.io.IOException
    {
        this.getAccess().readCF(src,reader);
        this.getName().readCF(src,reader);
        this.getType().readCF(src,reader);
        this.getAttributesChild().readCF(src,reader);
    }
    public void writeCF(IO.Uri dst, CF.Writer writer)
        throws java.io.IOException
    {
        this.getAccess().writeCF(dst,writer);
        this.getName().writeCF(dst,writer);
        this.getType().writeCF(dst,writer);
        this.getAttributesChild().writeCF(dst,writer);
    }
}
